﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using navigation.Data;
using navigation.Contents;
using System.Collections.ObjectModel;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation;

namespace navigation.Controls
{
    /// <summary>
    /// Interaction logic for TaskListControl.xaml
    /// </summary>
    public partial class TaskListControl : UserControl
    {
        public TaskListControl()
        {
            InitializeComponent();
        }

        #region Initialize DependencyProperty

        public static DependencyProperty UserStoryProperty = DependencyProperty.Register("UserStory", typeof(UserStory), typeof(TaskListControl));

        public UserStory UserStory
        {
            get
            {
                return (UserStory)GetValue(TaskListControl.UserStoryProperty);
            }
            set
            {
                SetValue(TaskListControl.UserStoryProperty, value);
            }
        }

        public static DependencyProperty TaskListProperty = DependencyProperty.Register("TaskList", typeof(ObservableCollection<Task>), typeof(TaskListControl));

        public ObservableCollection<Task> TaskList
        {
            get
            {
                return (ObservableCollection<Task>)GetValue(TaskListControl.TaskListProperty);
            }
            set
            {
                SetValue(TaskListControl.TaskListProperty, value);
            }
        }

        #endregion

        #region listbox events

        private void OnDragSourcePreviewTouchDown(object sender, TouchEventArgs e)
        {
            FrameworkElement findSource = e.OriginalSource as FrameworkElement;
            SurfaceListBoxItem draggedElement = null;

            // Find the touched SurfaceListBoxItem object.
            while (draggedElement == null && findSource != null)
            {
                if ((draggedElement = findSource as SurfaceListBoxItem) == null)
                {
                    findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
                }
            }

            if (draggedElement == null)
            {
                return;
            }

            // Create the cursor visual.
            ContentControl cursorVisual = new ContentControl()
            {
                Content = draggedElement.DataContext,
                Style = FindResource("CursorStyle") as Style
            };

            // Add a handler. This will enable the application to change the visual cues.
            //SurfaceDragDrop.AddTargetChangedHandler(cursorVisual, OnTargetChanged);

            // Create a list of input devices. Add the touches that
            // are currently captured within the dragged element and
            // the current touch (if it isn't already in the list).
            List<InputDevice> devices = new List<InputDevice>();
            devices.Add(e.TouchDevice);
            foreach (TouchDevice touch in draggedElement.TouchesCapturedWithin)
            {
                if (touch != e.TouchDevice)
                {
                    devices.Add(touch);
                }
            }

            // Get the drag source object
            ItemsControl dragSource = ItemsControl.ItemsControlFromItemContainer(draggedElement);

            SurfaceDragCursor startDragOkay =
                SurfaceDragDrop.BeginDragDrop(
                  dragSource,                 // The SurfaceListBox object that the cursor is dragged out from.
                  draggedElement,             // The SurfaceListBoxItem object that is dragged from the drag source.
                  cursorVisual,               // The visual element of the cursor.
                  draggedElement.DataContext, // The data associated with the cursor.
                  devices,                    // The input devices that start dragging the cursor.
                  DragDropEffects.Move);      // The allowed drag-and-drop effects of the operation.

            // If the drag began successfully, set e.Handled to true. 
            // Otherwise SurfaceListBoxItem captures the touch 
            // and causes the drag operation to fail.
            e.Handled = (startDragOkay != null);
            //draggedElement.Visibility = System.Windows.Visibility.Hidden;

            Task task = draggedElement.DataContext as Task;
            TaskList.Remove(task);
            
            if (task.State == "planned")
                UserStory.Planned.Remove(task);
            else if (task.State == "inprogress")
                UserStory.InProgress.Remove(task);
            else
                UserStory.Done.Remove(task);
        }

        private void TaskListBox_OnDropTargetDrop(object sender, SurfaceDragDropEventArgs e)
        {
            if ((e.Cursor.Data as Task) != null)
            {
                TaskList.Add(e.Cursor.Data as Task);
            }
        }
       
        #endregion
    }
}
